package cn.well.cloud.modular.service.impl;

import cn.stylefeng.roses.core.util.ToolUtil;
import cn.well.cloud.exception.TipsException;
import cn.well.cloud.exception.enums.TipsExceptionEnum;
import cn.well.cloud.modular.mapper.UserMapper;
import cn.well.cloud.modular.model.User;
import cn.well.cloud.modular.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhh
 * @since 2020-04-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Override
	public Long addUser(Object obj, String type,String nickName,String avatarUrl) {
		// 单线程添加用户
        synchronized (this) {
            JSONObject json = JSONObject.parseObject(obj.toString());
            int errcode = json.getIntValue("errcode");
            if (errcode != 0) {
                log.error(">>>" + obj.toString());
                throw new TipsException(TipsExceptionEnum.MINI_RESPONSE_EXCEPTION);
            }
            String openid = json.getString("openid");
            
            if(ToolUtil.isEmpty(openid)){
            	return null;
            }
            
            String unionid = json.getString("unionid");
            String sessionkey = json.getString("session_key");
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.eq("open_id", openid);
            if(ToolUtil.isNotEmpty(unionid)){
                queryWrapper.eq("union_id", unionid); //新用户 unionid 为空
            }
            User user = this.baseMapper.selectOne(queryWrapper);
            if (user == null) {
                    user = new User();
                    user.setOpenId(openid);
                    user.setNickName(nickName);
                    user.setHeadUrl(avatarUrl);
                    if(ToolUtil.isNotEmpty(unionid)){
                        user.setUnionid(unionid);
                    }
                    user.setSessionKey(sessionkey);
                    user.setCreateDate(new Date());
                    log.debug("----------------开始准备注册");
                    super.save(user);
            } else {
                UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
                updateWrapper.set("session_key", sessionkey);
                updateWrapper.set("unionid", unionid);
                updateWrapper.set("nick_name", nickName);
                updateWrapper.set("head_url", avatarUrl);
                updateWrapper.eq("user_id", user.getUserId());
                this.update(updateWrapper);
            }
            return user.getUserId();
        }
	}
	@Override
	public List<Map<String, Object>> listUserMap(String company,List<String> list,String mapId,String userId) {
		
		return this.baseMapper.listUserMap(company,list,mapId,userId);
	}
	@Override
	public List<Map<String, Object>> listUserStepById(String userId,
			String startDate, String endDate, String day) {
		
		return this.baseMapper.listUserStepById(userId,startDate,endDate,day);
	}
	@Override
	public int getUserAllStep(String userId) {
		
		return this.baseMapper.getUserAllStep(userId);
	}

}
